package com.shuqi.activity;

import java.util.List;

import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuqi.common.Urls;
import com.shuqi.common.utils.StringUtils;
import com.shuqi.common.utils.Util;
import com.shuqi.controller.MainController;
import com.shuqi.controller.R;
import com.shuqi.controller.RecommendController;
import com.shuqi.interfaces.EventTypeConstants;
import com.shuqi.interfaces.IEventListener;

/**
 * 网页书城 - 推荐
 * 
 * @author li.li
 * 
 */
public class BookSquareWebActivity extends BookCityWebBase implements IEventListener {

    private LinearLayout mRecommendNoticeLayout;// 公告栏布局
    private RecommendController mRecommendController;// 公告执行者

    @Override
    protected int contentView() {
        return R.layout.act_booksquare_web;
    }

    @Override
    public void activityLogicForward() {
        super.activityLogicForward();

        // 加载网页内容
        Util.loadUrl(this, mWebView, Urls.getWebShuqiRecommendUrl(this));
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                mRecommendController.handle(0, "notice");
            }
        }, 1000);
    }

    @Override
    public void activityInitData() {
        super.activityInitData();

        mRecommendNoticeLayout = (LinearLayout) findViewById(R.id.recommend_notice_layout);
        mRecommendController =
                (RecommendController) MainController.getMainManagerInstance().createController(
                        MainController.CONTROLLER_ID_RECOMMEND, this);
        mRecommendController.addEventListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()) {
            case R.id.recommend_notice_closedbut:// 关闭公告
                mRecommendNoticeLayout.setVisibility(View.GONE);
                break;
        }

    }

    /**
     * 回调监听
     */
    @Override
    public void onEvent(int event, Object data) {
        switch (event) {
            case EventTypeConstants.EVENT_SUCCESS:
                if (data != null) {
                    List<String> noticeInfo = extracted(data);
                    if (noticeInfo != null && noticeInfo.size() > 0) {
                        StringBuilder noticeSb = new StringBuilder();
                        for (String note : noticeInfo) {
                            noticeSb.append(note);
                        }
                        if (StringUtils.isNotBlank(noticeSb.toString())) {
                            TextView nt = (TextView) findViewById(R.id.recommend_notice_textview);
                            ImageButton close =
                                    (ImageButton) findViewById(R.id.recommend_notice_closedbut);
                            close.setOnClickListener(this);
                            nt.setText(noticeSb.toString());
                            nt.setSelected(true);

                            handler.post(new Runnable() {

                                @Override
                                public void run() {
                                    mRecommendNoticeLayout.setVisibility(View.VISIBLE);
                                }
                            });

                        }
                    } else {
                        handler.post(new Runnable() {

                            @Override
                            public void run() {
                                mRecommendNoticeLayout.setVisibility(View.GONE);
                            }
                        });

                    }
                }
                break;
        }
    }

    @SuppressWarnings("unchecked")
    private List<String> extracted(Object data) {
        if (data != null && data instanceof List)
            return (List<String>) data;

        return null;

    }
    
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                    BookShelfActivity.showFromMainActivityGroup(this, true);
                    ActivityManager.getInstance().finishActivity(this);
        }
        return super.onKeyDown(keyCode, event);
    }

}
